using System.Collections.Generic;
using UnityEngine;

public class CardLayoutManager : SingletonMono<CardLayoutManager>
{
    public GameObject upRightPlayer1;
    public GameObject downLeftPlayer1;
    public GameObject upRightPlayer2;
    public GameObject downLeftPlayer2;

    private Vector3 centerPointPlayer1;
    private Vector3 centerPointPlayer2;

    private const float MaxHeight = 2.5f;
    private const float CardSpacing = 0.5f;

    /// <summary>
    /// Awake is called when the script instance is being loaded.
    /// </summary>
    protected override void Awake()
    {
        base.Awake();
        centerPointPlayer1 = new Vector3((upRightPlayer1.transform.position.x + downLeftPlayer1.transform.position.x)/2, 
                (upRightPlayer1.transform.position.y + downLeftPlayer1.transform.position.y)/2 ,0 );
        
        centerPointPlayer2 = new Vector3((upRightPlayer2.transform.position.x + downLeftPlayer2.transform.position.x)/2, 
            (upRightPlayer2.transform.position.y + downLeftPlayer2.transform.position.y)/2 ,0 );
    }

    
    // 根据传入的卡片数量计算卡片的位置
    private Vector3 CalculateCardPositions(int index,int numberOfCards,ChessSide side)
    {
        var centerPoint = side == ChessSide.Player1 ? centerPointPlayer1 : centerPointPlayer2;
        // 计算当前卡片高度
        float currentHeight = CardSpacing * (numberOfCards - 1);
        // 计算卡片总高度，取当前高度和最大高度的最小值
        float totalHeight = Mathf.Min(currentHeight, MaxHeight); 
        float topCardYPos = 0;

        // 计算卡片之间的间距
        float currentSpacing = totalHeight > 0 ? totalHeight / (numberOfCards - 1) : 0;

        
        // 计算卡片X轴位置
        float yPos = topCardYPos - (index * currentSpacing);

        // 创建卡片位置向量
        return new Vector3(centerPoint.x, yPos, 0f);
    }

    public Vector3 GetCardTransform(int index,int totalCards,ChessSide side)
    {
        return CalculateCardPositions(index,totalCards,side);
    }

}
